package com.molyfun.parents.view.RecordView;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.molyfun.parents.R;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class ChatInputMenu extends LinearLayout {
    FrameLayout primaryMenuContainer;//, emojiconMenuContainer;
    protected ChatPrimaryMenuBase chatPrimaryMenu;
//    protected EaseEmojiconMenuBase emojiconMenu;
    protected ChatExtendMenu chatExtendMenu;
    protected FrameLayout chatExtendMenuContainer;
    protected LayoutInflater layoutInflater;

    private Handler handler = new Handler();
    private ChatInputMenuListener listener;
    private Context context;
    private boolean inited;

    public ChatInputMenu(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ChatInputMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ChatInputMenu(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_chat_input_menu, this);
        primaryMenuContainer = (FrameLayout) findViewById(R.id.primary_menu_container);
//        emojiconMenuContainer = (FrameLayout) findViewById(R.id.emojicon_menu_container);
        chatExtendMenuContainer = (FrameLayout) findViewById(R.id.extend_menu_container);

        // extend menu
        chatExtendMenu = (ChatExtendMenu) findViewById(R.id.extend_menu);

    }

    /**
     * init view
     *
     */
    public void init() {
        if(inited){
            return;
        }
        // primary menu, use default if no customized one
        if(chatPrimaryMenu == null){
            chatPrimaryMenu = (ChatPrimaryMenu) layoutInflater.inflate(R.layout.layout_chat_primary_menu, null);
        }
        primaryMenuContainer.addView(chatPrimaryMenu);

//        // emojicon menu, use default if no customized one
//        if(emojiconMenu == null){
//            emojiconMenu = (EaseEmojiconMenu) layoutInflater.inflate(R.layout.ease_layout_emojicon_menu, null);
//            if(emojiconGroupList == null){
//                emojiconGroupList = new ArrayList<EaseEmojiconGroupEntity>();
//                emojiconGroupList.add(new EaseEmojiconGroupEntity(R.drawable.ee_1,  Arrays.asList(EaseDefaultEmojiconDatas.getData())));
//            }
//            ((EaseEmojiconMenu)emojiconMenu).init(emojiconGroupList);
//        }
//        emojiconMenuContainer.addView(emojiconMenu);


        processChatMenu();
        chatExtendMenu.init();

        inited = true;
    }

    public void setEditTextHint(String text){
        chatPrimaryMenu.getEditText().setHint(text);
    }


    /**
     * set custom primary menu
     * @param customPrimaryMenu
     */
    public void setCustomPrimaryMenu(ChatPrimaryMenuBase customPrimaryMenu){
        this.chatPrimaryMenu = customPrimaryMenu;
    }

    public ChatPrimaryMenuBase getPrimaryMenu(){
        return chatPrimaryMenu;
    }

    public ChatExtendMenu getExtendMenu(){
        return chatExtendMenu;
    }


    /**
     * register menu item
     *
     * @param name
     *            item name
     * @param drawableRes
     *            background of item
     * @param itemId
     *             id
     * @param listener
     *            on click event of item
     */
    public void registerExtendMenuItem(String name, int drawableRes, int itemId,
                                       ChatExtendMenu.ChatExtendMenuItemClickListener listener) {
        chatExtendMenu.registerMenuItem(name, drawableRes, itemId, listener);
    }

    /**
     * register menu item
     *
     * @param nameRes
     *            resource id of item name
     * @param drawableRes
     *            background of item
     * @param itemId
     *             id
     * @param listener
     *            on click event of item
     */
    public void registerExtendMenuItem(int nameRes, int drawableRes, int itemId,
                                       ChatExtendMenu.ChatExtendMenuItemClickListener listener) {
        chatExtendMenu.registerMenuItem(nameRes, drawableRes, itemId, listener);
    }


    protected void processChatMenu() {
        // send message button
        chatPrimaryMenu.setChatPrimaryMenuListener(new ChatPrimaryMenuBase.ChatPrimaryMenuListener() {

            @Override
            public void onSendBtnClicked(String content) {
                if (listener != null)
                    listener.onSendMessage(content);
            }

            @Override
            public void onToggleVoiceBtnClicked() {
                hideExtendMenuContainer();
            }

            @Override
            public void onToggleExtendClicked() {
                toggleMore();
            }

            @Override
            public void onEditTextClicked() {
                hideExtendMenuContainer();
            }

            @Override
            public void onToggleEmojiconClicked() {
//                toggleEmojicon();
            }

            @Override
            public boolean onPressToSpeakBtnTouch(View v, MotionEvent event) {
                if(listener != null){
                    return listener.onPressToSpeakBtnTouch(v, event);
                }
                return false;
            }
        });

//        emojiconMenu.setEmojiconMenuListener(new EaseEmojiconMenuBase.EaseEmojiconMenuListener() {
//
//            @Override
//            public void onExpressionClicked(EaseEmojicon emojicon) {
//                if(emojicon.getType() != EaseEmojicon.Type.BIG_EXPRESSION){
//                    if(emojicon.getEmojiText() != null){
//                        chatPrimaryMenu.onEmojiconInputEvent(EaseSmileUtils.getSmiledText(context,emojicon.getEmojiText()));
//                    }
//                }else{
//                    if(listener != null){
//                        listener.onBigExpressionClicked(emojicon);
//                    }
//                }
//            }
//
//            @Override
//            public void onDeleteImageClicked() {
//                chatPrimaryMenu.onEmojiconDeleteEvent();
//            }
//        });


    }


    /**
     * insert text
     * @param text
     */
    public void insertText(String text){
        getPrimaryMenu().onTextInsert(text);
    }

//    /**
//     * show or hide emojicon
//     */
//    protected void toggleEmojicon() {
//        if (chatExtendMenuContainer.getVisibility() == View.GONE) {
//            hideKeyboard();
//            handler.postDelayed(new Runnable() {
//                public void run() {
//                    chatExtendMenuContainer.setVisibility(View.VISIBLE);
//                    chatExtendMenu.setVisibility(View.GONE);
//                    emojiconMenu.setVisibility(View.VISIBLE);
//                }
//            }, 50);
//        } else {
//            if (emojiconMenu.getVisibility() == View.VISIBLE) {
//                chatExtendMenuContainer.setVisibility(View.GONE);
//                emojiconMenu.setVisibility(View.GONE);
//            } else {
//                chatExtendMenu.setVisibility(View.GONE);
//                emojiconMenu.setVisibility(View.VISIBLE);
//            }
//
//        }
//    }

    /**
     * show or hide extend menu
     *
     */
    protected void toggleMore() {
        if (chatExtendMenuContainer.getVisibility() == View.GONE) {
            hideKeyboard();
            handler.postDelayed(new Runnable() {
                public void run() {
                    chatExtendMenuContainer.setVisibility(View.VISIBLE);
                    chatExtendMenu.setVisibility(View.VISIBLE);
                }
            }, 50);
        } else {
            chatExtendMenuContainer.setVisibility(View.GONE);

        }
    }


    /**
     * hide keyboard
     */
    private void hideKeyboard() {
        chatPrimaryMenu.hideKeyboard();
    }

    /**
     * hide extend menu
     */
    public void hideExtendMenuContainer() {
        chatExtendMenu.setVisibility(View.GONE);
        chatExtendMenuContainer.setVisibility(View.GONE);
        chatPrimaryMenu.onExtendMenuContainerHide();
    }

    /**
     * when back key pressed
     *
     * @return false--extend menu is on, will hide it first
     *         true --extend menu is off
     */
    public boolean onBackPressed() {
        if (chatExtendMenuContainer.getVisibility() == View.VISIBLE) {
            hideExtendMenuContainer();
            return false;
        } else {
            return true;
        }

    }


    public void setChatInputMenuListener(ChatInputMenuListener listener) {
        this.listener = listener;
    }

    public interface ChatInputMenuListener {
        /**
         * when send message button pressed
         *
         * @param content
         *            message content
         */
        void onSendMessage(String content);

//        void onBigExpressionClicked(EaseEmojicon emojicon);

        /**
         * when speak button is touched
         * @param v
         * @param event
         * @return
         */
        boolean onPressToSpeakBtnTouch(View v, MotionEvent event);
    }

}